@extends('admin.tableLayout')
@section('title','栏目列表')
@section('barName','栏目列表')
@section('quote')
    <button type="button" class="layui-btn layui-btn-small" id="getAll" onclick="javascript:location.href='{{route('admin.category-add')}}'"><i class="fa fa-plus" aria-hidden="true"></i> 添加栏目</button>
    <hr/>
<!--    <form class="layui-form" >

            <label>状态：</label>
        <div class="layui-inline">
            <select name="status"  id="status" lay-search lay-filter="select">
                <option value="">全部</option>
                @foreach($status as $k=>$v)
                    <option value="{{$k}}">{{$v}}</option>
                @endforeach
            </select>

        </div>
        </form> -->
@endsection

@section('checkbox_btn')
    @can('posts')
    <button class="layui-btn" data-type="deleteAll">批量删除</button>

    @endcan
@endsection
@section('toolbar')
    @can('posts')
    @{{#  if(d.topid !=0){ }}
    <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="top"><i class="fa fa-eye"></i> 查看顶级</a>
    @{{#  } }}
    @{{#  if(d.pid !=0){ }}
    <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="parent"><i class="fa fa-eye"></i> 查看父级</a>
    @{{# } }}
    @{{#  if(d.ispart==1){ }}
    <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="son"><i class="fa fa-eye"></i> 查看子级</a>


    @{{#  } }}

    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>

    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>

    @endcan
@endsection
@section('tableRender')
    <script type="text/html" id="status_name">
        @{{#  if(d.status_name == '显示'){ }}
        <a href="javascript:void(0)" lay-event="draft" style="color:#03a9f4">{{$categoryManager->getStatusString(\App\Service\Category::DISPLAY)}}</a>
        @{{#  } else { }}
        <a href="javascript:void(0)"  lay-event="published" style="color:#03a9f4">{{$categoryManager->getStatusString(\App\Service\Category::DISPLAY_NONE)}}</a>
        @{{#  } }}
    </script>
        <script>
        layui.use(['table','jquery','form'], function(){
            var table = layui.table;
            var $=layui.jquery;
            var form=layui.form;
            var _renderLoad = layer.msg('正在查找栏目，请稍候',{icon: 16,time:false,shade:0.8});
            var render=table.render({
                elem:"#table",
                page:true,
                id:'idTest',
                url:'{{route('admin.categoryAjax')}}',
                where:{
                    status:$("#status").val(),
                    pid:0
                },
                height:400,
                cols:  [[ //标题栏
                    {checkbox:true,fixed:true},
                    {field:'sort',title:'排序<i class="fa fa-paint-brush"></i>',width:80,edit:'text',fixed:true},
                    {field: 'id', title: 'ID', width: 80,fixed:true},
                    {field: 'cat_name', title: '栏目名称', width: 150},
                    {field:'parent_name',title:'父栏目',width:150},
                    {field:'top_name',title:'顶级栏目',width:150},
                    {field:'slug',title:'slug',width:80},
                    {field:'status_name',title:'状态',width:80,templet:'#status_name'},
                    {fixed: 'right', width:400,title:'操作', align:'center', toolbar: '#toolbar'}
                    //这里的toolbar值是模板元素的选择器
                ]],
                response: {//返回格式自定义
                    statusName: 'code' //数据状态的字段名称，默认：code
                    ,statusCode: 0 //成功的状态码，默认：0
                    ,msgName: 'msg' //状态信息的字段名称，默认：msg
                    ,countName: 'total' //数据总数的字段名称，默认：count
                    ,dataName: 'data' //数据列表的字段名称，默认：data
                },
                done:function () {
                    layer.close(_renderLoad);
                }
            });
            $('#search').on('click',function () {
                var data = {
                    status:$("#status").val(),
                };
                _renderLoad = layer.msg('正在查找栏目，请稍候',{icon: 16,time:false,shade:0.8});
                render.reload(
                    {
                        where:data
                    }
                );
                return false;
            })
            table.on('checkbox(table)', function(obj){
                //console.log(obj)
            });


            //上方
            var   active = {
                deleteAll: function(){ //获取选中数据
                    var checkStatus = table.checkStatus('idTest')
                        ,data = checkStatus.data;
                    var str=[];
                    if(data==''){
                        layer.msg('请选中所删除的行',{icon:5})
                        return false;
                    }
                    for(var i=0;i<data.length;i++){
                        str.push(data[i].id);
                    }

                    layer.confirm('是否删除所选',function(index) {

                        $.get('{{route('admin.category',['act'=>'delete'])}}', {id: str,type:1}, function (obj) {
                            if (obj.info == 'success') {
                                layer.msg('操作成功', {icon: 1});
                                render.reload();
                            } else {
                                layer.msg('网络繁忙', {icon: 5});
                            }

                        })
                    })

                },
                draftAll: function(){ //获取选中数目
                    var checkStatus = table.checkStatus('idTest')
                        ,data = checkStatus.data;
                    var str=[];
                    if(data==''){
                        layer.msg('请选中所要隐藏的行',{icon:5})
                        return false;
                    }
                    for(var i=0;i<data.length;i++){
                        str.push(data[i].id);
                    }
                    layer.confirm('是否将选中的栏目取消展示',function(index){
                        layer.close(index);
                        //服务端发送审核指令
                        $.get('{{route('admin.category',['act'=>'draft'])}}',{id:str,type:1},function(obj){
                            if(obj.info=='success'){
                                layer.msg('操作成功',{icon:1});
                                render.reload();
                            }else{
                                layer.msg('网络繁忙',{icon:5});
                            }

                        })
                    });

                }
                ,
                publishedAll: function(){ //获取选中数目

                    var checkStatus = table.checkStatus('idTest')
                        ,data = checkStatus.data;
                    var str=[];
                    if(data==''){
                        layer.msg('请选中所展示的行',{icon:5})
                        return false;
                    }
                    for(var i=0;i<data.length;i++){
                        str.push(data[i].id);
                    }
                    layer.confirm('是否展示选中栏目',function(index){
                        layer.close(index);
                        //服务端发送审核指令
                        $.get('{{route('admin.category',['act'=>'published'])}}',{id:str,type:1},function(obj){
                            if(obj.info=='success'){
                                layer.msg('操作成功',{icon:1});
                                render.reload();
                            }else{
                                layer.msg('网络繁忙',{icon:5});
                            }

                        })
                    });

                }
                ,isAll: function(){ //验证是否全选
                    var checkStatus = table.checkStatus('idTest');
                    layer.msg(checkStatus.isAll ? '全选': '未全选')
                }
            };
            $('.demoTable .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
            table.on('edit(table)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
                $.get('{{route('admin.category',['act'=>'sortCol'])}}',{id:obj.data.id,sort:obj.value},function (e) {
                },'json')
            });
            table.on('tool(table)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值
                var tr = obj.tr; //获得当前行 tr 的DOM对象
                 if(layEvent === 'delete'){ //删除
                    layer.confirm('真的删除行么', function(index){
                        layer.close(index);
                        //向服务端发送删除指令
                        $.get('{{route('admin.category',['act'=>'delete'])}}',{id:data.id},function(e){
                            if(e.info=='success'){
                                layer.msg('操作成功',{icon:1});
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            }
                        });
                    });
                } else if(layEvent === 'edit'){ //编辑
                     location.href='/admin/categories/edit/'+data.id;
                    //do something
                    //同步更新缓存对应的值
                }else if(layEvent === 'draft'){
                    layer.confirm('取消栏目展示',function(index){
                        layer.close(index);
                        //服务端发送审核指令
                        $.get('{{route('admin.category',['act'=>'draft'])}}',{id:data.id},function(e){
                            if(e.info=='success'){
                                layer.msg('操作成功',{icon:1});
                                render.reload();
                            }else{
                                layer.msg('网络繁忙',{icon:5});
                            }

                        })
                    });

                }else if(layEvent=='published'){
                    layer.confirm('是否展示栏目',function(index){
                        layer.close(index);
                        //服务端发送审核指令
                        $.get('{{route('admin.category',['act'=>'published'])}}',{id:data.id},function(e){
                            if(e.info=='success'){
                                layer.msg('操作成功',{icon:1});
                                render.reload();
                            }else{
                                layer.msg('网络繁忙',{icon:5});
                            }

                        })
                    });
                }else if(layEvent === 'son'){
                     var posts = {
                         pid:data.id,
                         status:$("#status").val()
                     };
                     _renderLoad = layer.msg('正在查找栏目，请稍候',{icon: 16,time:false,shade:0.8});
                     render.reload(
                         {
                             where:posts
                         }
                     );

                 }else if(layEvent === 'top'){
                     var posts = {
                         pid:0,
                         status:$("#status").val(),
                     };
                     _renderLoad = layer.msg('正在查找栏目，请稍候',{icon: 16,time:false,shade:0.8});
                     render.reload(
                         {
                             where:posts
                         }
                     );
                 }else if(layEvent === 'parent'){
                     var posts = {
                         pid:data.ppid,
                         status:$("#status").val(),
                     };
                     _renderLoad = layer.msg('正在查找栏目，请稍候',{icon: 16,time:false,shade:0.8});
                     render.reload(
                         {
                             where:posts
                         }
                     );
                 }
            });
        });
    </script>
@endsection

